<?xml version="1.0" encoding="UTF-8"?>
<!-- flat to nested -->
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/root">
		<root>
			<xsl:apply-templates select="item[@parent='0']" />
		</root>
	</xsl:template>
	
	<xsl:template match="item">
		<item id="{@id}">
			<xsl:apply-templates select="/root/item[@parent=current()/@id]" />
		</item>
	</xsl:template>
</xsl:stylesheet>